import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from flask import Flask
from models.models import db, PaymentSummary

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'instance', 'school.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

def migrate_payment_summary_data():
    with app.app_context():
        summaries = PaymentSummary.query.all()
        
        for summary in summaries:
            # 创建新的fees字典
            fees = {}
            
            # 将旧的字段数据转移到fees字典中
            if hasattr(summary, 'deposit') and summary.deposit:
                fees['定金'] = float(summary.deposit)
            if hasattr(summary, 'activity_fee') and summary.activity_fee:
                fees['延时实践活动及材料费'] = float(summary.activity_fee)
            if hasattr(summary, 'food_fee') and summary.food_fee:
                fees['伙食费'] = float(summary.food_fee)
            if hasattr(summary, 'video_fee') and summary.video_fee:
                fees['视频费'] = float(summary.video_fee)
            if hasattr(summary, 'book_fee') and summary.book_fee:
                fees['书费'] = float(summary.book_fee)
            if hasattr(summary, 'uniform_fee') and summary.uniform_fee:
                fees['园服'] = float(summary.uniform_fee)
            if hasattr(summary, 'bedding_fee') and summary.bedding_fee:
                fees['床上用品'] = float(summary.bedding_fee)
            if hasattr(summary, 'proxy_fee') and summary.proxy_fee:
                fees['代收费'] = float(summary.proxy_fee)
            if hasattr(summary, 'tuition_fee') and summary.tuition_fee:
                fees['保教费'] = float(summary.tuition_fee)
            
            # 更新fees字段
            summary.fees = fees
            
            # 重新计算total
            summary.total = sum(fees.values())
        
        # 提交更改
        db.session.commit()

if __name__ == '__main__':
    migrate_payment_summary_data() 